package com.codingke.codingbookstore.ui;

import android.content.Intent;
import android.net.Uri;
import android.os.Bundle;
import android.os.Environment;
import android.provider.MediaStore;
import android.view.View;
import android.widget.LinearLayout;

import com.codingke.codingbookstore.R;
import com.codingke.codingbookstore.vo.User;

import java.io.File;

import cn.bmob.v3.BmobUser;
import cn.bmob.v3.datatype.BmobFile;
import cn.bmob.v3.listener.UpdateListener;
import cn.bmob.v3.listener.UploadFileListener;

@Deprecated
public class UserSettingsActivity extends BaseActivity implements View.OnClickListener {

    private static final int PHOTO_REQUEST_GALLERY = 0x100;
    private static final int PHOTO_REQUEST_CUT = 0x200;
    private LinearLayout set_username_layout, set_userIcon_layout;
    private File tempImage; //裁剪后的图片

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_user_settings);
        set_username_layout = (LinearLayout) findViewById(R.id.set_username_layout);
        set_userIcon_layout = (LinearLayout) findViewById(R.id.set_userIcon_layout);
        set_username_layout.setOnClickListener(this);
        set_userIcon_layout.setOnClickListener(this);

    }

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.set_username_layout:
                startActivity(new Intent(this, SettingsUsernameActivity.class));
                break;
            case R.id.set_userIcon_layout:
                selectIcon();
                break;
        }
    }

    private void selectIcon() {
        tempImage = new File(Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_PICTURES) + "/"+ System.currentTimeMillis()+".jpg");
        Intent intent = new Intent(Intent.ACTION_GET_CONTENT);
        intent.setType("image/*");
        // crop为true是设置在开启的intent中设置显示的view可以剪裁
        intent.putExtra("crop", "true");
        // aspectX aspectY 是宽高的比例
        intent.putExtra("aspectX", 1);
        intent.putExtra("aspectY", 1);
        intent.putExtra("scale", true);
        // outputX,outputY 是剪裁图片的宽高
        intent.putExtra("outputX", 200);
        intent.putExtra("outputY", 200);
        intent.putExtra("return-data", false);
        intent.putExtra(MediaStore.EXTRA_OUTPUT, Uri.fromFile(tempImage));
        intent.putExtra("noFaceDetection", true);
        startActivityForResult(intent, PHOTO_REQUEST_CUT);
    }


    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        if (RESULT_OK == resultCode && PHOTO_REQUEST_CUT == requestCode) {

            if (tempImage.exists()) {
                final User user = BmobUser.getCurrentUser(this, User.class);
                final BmobFile bmobFile = new BmobFile(tempImage);
                bmobFile.upload(this, new UploadFileListener() {
                    @Override
                    public void onSuccess() {
                        user.setUserIcon(bmobFile);
                        user.update(UserSettingsActivity.this, new UpdateListener() {
                            @Override
                            public void onSuccess() {
                                showToast("头像更新成功");
                            }

                            @Override
                            public void onFailure(int i, String s) {
                                showToast("头像更新失败" + s);
                            }
                        });

                    }

                    @Override
                    public void onFailure(int i, String s) {
                        showToast("头像更新失败");
                    }
                });

            }
            //删除图片
            tempImage.delete();
        }
    }
}
